﻿using PMSystem.DataAccess.DataContext;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace PMSystem.DataAccess.DataRepository
{
    public class RolesRepository : IRepository<Role>
    {
        Context context = new Context();

        public IQueryable<Role> GetAll
        {
            get { return context.Roles; }
        }

        public IQueryable<Role> AllIncluding(params Expression<Func<Role, object>>[] includeProperties)
        {

            IQueryable<Role> query = context.Roles;
            foreach (var includeProperty in includeProperties)
            {
                // query = query.Include(includeProperty);
            }
            return query;
        }



        public void InsertOrUpdate(Role record)
        {
            if (record.RoleId == default(int))
            {
                // New entity
                context.Roles.Add(record);
            }
            else
            {
                // Existing entity
                context.Entry(record).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var role = context.Roles.Find(id);
            context.Roles.Remove(role);
        }

        public void Save()
        {
            context.SaveChanges();
        }
        public void Dispose()
        {
            context.Dispose();
        }

        public Role GetById(int id)
        {
            return context.Roles.Find(id);
        }

        public List<Role> GetAllByCondition(Expression<Func<Role, bool>> predicate)
        {
            return context.Roles.Where(predicate).ToList();
        }

    }

    public class UserRolesRepository : IRepository<UserRole>
    {
        Context context = new Context();

        public IQueryable<UserRole> GetAll
        {
            get { return context.UserRoles; }
        }

        public IQueryable<UserRole> AllIncluding(params Expression<Func<UserRole, object>>[] includeProperties)
        {

            IQueryable<UserRole> query = context.UserRoles;
            foreach (var includeProperty in includeProperties)
            {
                // query = query.Include(includeProperty);
            }
            return query;
        }



        public void InsertOrUpdate(UserRole record)
        {
            if (record.UserRoleId == default(int))
            {
                // New entity
                context.UserRoles.Add(record);
            }
            else
            {
                // Existing entity
                context.Entry(record).State = EntityState.Modified;
            }
        }

        public void Delete(int id)
        {
            var UserRole = context.UserRoles.Find(id);
            context.UserRoles.Remove(UserRole);
        }

        public void Save()
        {
            context.SaveChanges();
        }
        public void Dispose()
        {
            context.Dispose();
        }

        public UserRole GetById(int id)
        {
            return context.UserRoles.Find(id);
        }

        public List<UserRole> GetAllByCondition(Expression<Func<UserRole, bool>> predicate)
        {
            return context.UserRoles.Where(predicate).ToList();
        }

        public virtual void DeleteList(List<UserRole> items)
        {

            foreach (UserRole item in items)
            {
                context.Entry(item).State = EntityState.Deleted;
            }
            context.SaveChanges();

        }

    }
}
